<?php
/*
*后台属性展示
*/
namespace app\admin\controller;
use think\Controller;
use think\Request;
use \think\db;
use app\admin\lib\Tree;
class Product extends Base{
    //车辆列表
    public function index(){
      $db = Db::table('jun_product');
      $list = $db->order('sort asc')->paginate(10);
      $page = $list->render();
      // 模板变量赋值
      $this->assign('list', $list);
      $this->assign('page', $page);
      return $this->fetch();
    }
    //品牌列表
    public function classlist(){
       $db = Db::table('jun_product_class');
       $list = $db->order('name asc')->select();
       $lists =  Tree::findmenu($list,'---');
       // dump($lists);
       $this->assign('list',$lists);

        return $this->fetch();
     }
     //品牌添加
    public function addclass(){
       if ($_POST['name']=='') {
         $this->error('请上传品名称');die;
       }
      $file = request()->file('pic');
          if($file){
              $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
              if($info){
                  $_POST['pic'] = '/public/uploads/'.$info->getSaveName();
              }
          }
       $ok = Db::table('jun_product_class')->insert($_POST);
       if ($ok) {
         $this->success('操作成功');
       }else{
         $this->error('操作失败');
       }
     }
     //删除品牌
    public function delclass(){
       $request = Request::instance();//getid
       $getid = $request->route();
       $ok = Db::table('jun_product_class')->where('id',$getid['id'])->delete();
       if($ok){
         $this->success('删除成功');
       }else{
         $this->success('删除失败');
       }
     }
    //异步提交
    public function upclass(){
      $db = Db::table('jun_product_class');
      $name = $_POST['name'];
      $id = $_POST['id'];
      $ok = $db->where('id',$id)->update(['name'=> $name]);
      if($ok){
        $arr = ['sutart'=>'操作成功'];
        echo json_encode($arr);
      }else{
        $arr = ['sutart'=>'操作失败'];
        echo json_encode($arr);
      }
     }
     //异步更改id
     //异步上传图片
    public function upclass_uid(){
      $db = Db::table('jun_product_class');
      $name = $_POST['name'];
      $id = $_POST['id'];
      $ok = $db->where('id',$id)->update(['uid'=> $name]);
      if($ok){
        $arr = ['sutart'=>'操作成功'];
        echo json_encode($arr);
      }else{
        $arr = ['sutart'=>'操作失败'];
        echo json_encode($arr);
      }
     }
    //品牌添加
    public function addclass_pic(){
        $file = request()->file('pic');
          if($file){
              $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
              if($info){
                  $pic = '/public/uploads/'.$info->getSaveName();
              }
          }
         $result = Db::table('jun_product_class')
          ->where('id',$_POST['id'])
          ->update(['pic'=>$pic]);
         if($result){
             $arr = [
               'status'=>1,
               'imgurl'=>$pic
             ];
         }else{
           $arr = array(
               'status'=>0
            );

         }
          echo json_encode($arr);
       }

     //添加二级菜单
    //添加二级菜单
    public function addlevel(){
          $db = Db::table('jun_product_class');
          $request = Request::instance();//getid
          $getid = $request->route();
          $ok = $db->insert(['name'=>'新二级菜单','uid'=>$getid['id']]);
          if ($ok) {
            $this->success('添加成功');
          }else{
            $this->error('添加失败');
          }
     }
    //添加车辆
    public function addproduct(){
      // getid
      $request = Request::instance();//getid
      $getid = $request->route();
      $product = Db::table('jun_product');
      if ($getid) {
        //查询数据库有没有对应的json
        $li = $product->where('id',$li)->find();
        // 首付比例
        $att_arr = json_decode($li['dow_payments'],true);
        $this->assign('attr',$att_arr);
        // 还款年限
        $repayments = json_decode($li['repayments'],true);
        $this->assign('nianxian',$repayments);
        $this->assign('attr',$att_arr);
        // 商业保障
        $baozhang = json_decode($li['commercial_insurance'],true);
        $this->assign('baozhang',$baozhang);
        // 必要收费
        $huafei = json_decode($li['necessary_charges'],true);
        $this->assign('huafei',$huafei);
      }else{
        //查询首付
        $att_arr = $this->attribute(1);
        $this->assign('attr',$att_arr);
        // 还款年限
        $repayments = $this->attribute(2);
        $this->assign('nianxian',$repayments);
        // 商业保障
        $baozhang = $this->attribute(3);
        $this->assign('baozhang',$baozhang);
        // 必要收费
        $huafei = $this->attribute(4);
        $this->assign('huafei',$huafei);

      }



      // dump($att_arr);
      return $this->fetch();
    }
    //通用查询属性表
    public function attribute($id){
        $attribute = Db::table('jun_attribute')->where('id',$id)->find();
        $att_arr = json_decode($attribute['content'],true);
        return $att_arr;
    }

    //查询车辆信息
    public function select_che(){
      $file_path = "/Applications/MAMP/htdocs/public/rand.txt";
              if(file_exists($file_path)){
                  $str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
                  $strs = explode("\r\n", $str);
              }
              $data = json_decode($strs[0],true);
              $arr = $data['result'];
              foreach ($arr as $key => $value) {
                  $arrd[] = $value;
              }
              // $this->addclass_a($arrd);
              // dump($arrd);
    }

    //循环
  public function addclass_a($arr,$uid = '0'){
      $db = Db::name('product_class');
      foreach ($arr as $key => $value) {
        if(isset($value['L'])){
          $L =$value['L'];
        }else{
          $L =0;
        }
          $data = [
            'name' =>$value['N'],
             'uid' => $uid,
             'i' => $value['I'],
             'l' => $L,
           ];
           $id = $db->insertGetId($data);
          if (isset($arr[$key]['List'])) {
           self::addclass_a($value['List'],$id);
          }
      }
    }

    //查询车辆信息
  public function select_subclass(){
      $file_path = "/Applications/MAMP/htdocs/public/class.txt";
              if(file_exists($file_path)){
                  $str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
                  $strs = explode("\r\n", $str);
              }

              dump($strs);
    }

    //循环型号
    // public function addclass_xinghao($arr,$uid = '0'){
    //   $db = Db::name('product_subclass');
    //   foreach ($arr as $key => $value) {
    //     if(isset($value['L'])){
    //       $L =$value['L'];
    //     }else{
    //       $L =0;
    //     }
    //       $data = [
    //         'name' =>$value['N'],
    //          'uid' => $uid,
    //          'i' => $value['I'],
    //          'l' => $L,
    //        ];
    //        $id = $db->insertGetId($data);
    //       if (isset($arr[$key]['List'])) {
    //        self::addclass_a($value['List'],$id);
    //       }
    //   }
    // }
}
